//
//  DataManager.m
//  Rockval
//
//  Created by TuanNT on 4/27/14.
//  Copyright (c) 2014 TNL. All rights reserved.
//

#import "DataManager.h"
#import "APIManager.h"

@implementation DataManager

static DataManager *SINGLETON = nil;

static bool isFirstAccess = YES;

#pragma mark - Public Method

+ (id)sharedInstance {
	static dispatch_once_t onceToken;
	dispatch_once(&onceToken, ^{
	    isFirstAccess = NO;
	    SINGLETON = [[super allocWithZone:NULL] init];
	});

	return SINGLETON;
}

#pragma mark - Life Cycle

+ (id)allocWithZone:(NSZone *)zone {
	return [self sharedInstance];
}

+ (id)copyWithZone:(struct _NSZone *)zone {
	return [self sharedInstance];
}

+ (id)mutableCopyWithZone:(struct _NSZone *)zone {
	return [self sharedInstance];
}

- (id)copy {
	return [[DataManager alloc] init];
}

- (id)mutableCopy {
	return [[DataManager alloc] init];
}

- (id)init {
	if (SINGLETON) {
		return SINGLETON;
	}
	if (isFirstAccess) {
		[self doesNotRecognizeSelector:_cmd];
	}
	self = [super init];
	return self;
}

//================================ METHODS ================================
#pragma mark -
#pragma mark METHODS
+ (NSMutableDictionary*) getFullContries{
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Countries" ofType:@"plist"];
	NSMutableDictionary *values = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
    return values;
}
- (void)loginWithEmail:(NSString *)email
              password:(NSString *)password
              andBlock:(void (^)(BOOL isSuccess))block {
	[[APIManager sharedInstance] loginWithEmail:email password:password andBlock: ^(BOOL isSuccess, NSDictionary *accountDict) {
	    self.account = [Account modelObjectWithDictionary:accountDict];
	    block(isSuccess);
	}];
}

- (void)watchingListForSaleWithBlock:(void (^)(NSArray *list))block {
	[[APIManager sharedInstance] watchingListForSaleWithCustomerId:self.account.customerId andBlock: ^(NSArray *list) {
	    block(list);
	}];
}

- (void)watchingListForLeaseWithBlock:(void (^)(NSArray *list))block {
	[[APIManager sharedInstance] watchingListForLeaseWithCustomerId:self.account.customerId andBlock: ^(NSArray *list) {
	    block(list);
	}];
}

- (void)addToWatchingListForItem:(NSString *)itemId andBlock:(void (^)(BOOL result))block {
	[[APIManager sharedInstance] addToWatchingListForItem:itemId customerId:self.account.customerId andBlock: ^(BOOL result) {
	    block(result);
	}];
}

- (void)removeFromWatchingListForItem:(NSString *)itemId andBlock:(void (^)(BOOL result))block {
	[[APIManager sharedInstance] removeFromWatchingListForItem:itemId customerId:self.account.customerId andBlock: ^(BOOL result) {
	    block(result);
	}];
}

- (void)recentActivityForCustomerWithId:(NSString *)customerId block:(void (^)(NSArray *list))block {
	[[APIManager sharedInstance] recentActivityForCustomerWithId:customerId block: ^(NSArray *list) {
	    block(list);
	}];
}

- (void)saleNearByWithLatitute:(NSString *)lat
                    longtitute:(NSString *)longt
                      andBlock:(void (^)(NSArray *list))block {
	[[APIManager sharedInstance] saleNearByWithLatitute:lat
	                                         longtitute:longt
	                                           andBlock: ^(NSArray *list) {
	    block(list);
	}];
}

- (void)leaseNearByWithLatitute:(NSString *)lat
                     longtitute:(NSString *)longt
                       andBlock:(void (^)(NSArray *list))block {
	[[APIManager sharedInstance] leaseNearByWithLatitute:lat
	                                          longtitute:longt
	                                            andBlock: ^(NSArray *list) {
	    block(list);
	}];
}

@end
